<?php declare(strict_types=1);


namespace App\Api\V2\Controller\Vbill\Alipay;


use App\Api\Controllers\BaseController;
use App\Api\Controllers\Config\VbillConfigController;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AuthenticationController extends BaseController
{

    use ResponseTrait;

    public function certification(Request $request)
    {
        $store_id = $request->input('store_id', ''); // 门店id
        if (!$store_id) {
            return $this->errorResponse("门店ID不能为空");
        }
        $contactName = $request->input('contactName', ''); // 联系人姓名
        $phoneNumber = $request->input('phoneNumber', ''); // 联系人手机号
        $contactIdNo = $request->input('contactIdNo', ''); // 联系人身份证号

        $JdStoreItem = Db::table('vbill_stores')->where(['store_id' => $store_id])->get()->toArray();
        if (!$JdStoreItem) {
            return json_encode([
                'status' => 2,
                'message' => '随行付mno不存在'
            ]);
        }
//        print_r($JdStoreItem[0]);die;

        $config = new VbillConfigController();
        $vbill_config = $config->vbill_config($JdStoreItem[0]->config_id);
//        dump($vbill_config->sxfpublic);die;

        $obj = new \App\Api\Controllers\Vbill\BaseController();
        $url = $obj->alipay_commit_apply;
//        dump($url);die;

//        $JdStoreItem[0]->mno = '399220517423529';
//        $JdStoreItem[0]->applicationId = 'f76c284da5644575aabe94043ccb637d';

        $data = [
            'orgId' => $vbill_config->orgId,
            'reqId' => time(),
            'version' => '1.0',
            'timestamp' => date('Ymdhis', time()),
            'signType' => 'RSA',
            'reqData' => [
//                'applicationId' => $JdStoreItem[0]->applicationId, // f76c284da5644575aabe94043ccb637d
                'mno' => $JdStoreItem[0]->mno,
                // 'contactName' => '童大伟',
                // 'phoneNumber' => '15601860037',
                // 'contactIdNo' => '342423199007116071',

            ],
        ];
        // dump($data);die;
        $res = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
        Log::channel("vbill")->info("certification_ali", [
            "url" => $url,
            "req" => $data,
            "res" => $res
        ]);
        // dump($res);die;
        if ($res['data']['respData']['bizCode'] == '0000') {
            return json_encode([
                'status' => 1,
                'message' => '操作成功'
            ]);
        } else {
            return json_encode([
                'status' => 2,
                'message' => $res['data']['respData']['bizMsg']
            ]);
        }
    }

    /**
     * 实名认证申请结果查询
     * 2022-07-20 16:12
     */
    public function queryApplyInfo(Request $request)
    {
        $store_id = $request->input('store_id', ''); // 门店id
        if (!$store_id) {
            return $this->errorResponse("门店ID不能为空");
        }
        $contactName = $request->input('contactName', ''); // 联系人姓名
        $phoneNumber = $request->input('phoneNumber', ''); // 联系人手机号
        $contactIdNo = $request->input('contactIdNo', ''); // 联系人身份证号

        $JdStoreItem = Db::table('vbill_stores')->where(['store_id' => $store_id])->get()->toArray();
        if (!$JdStoreItem) {
            return json_encode([
                'status' => 2,
                'message' => '随行付未开通，请先开通通道'
            ]);
        }

        $config = new VbillConfigController();
        $vbill_config = $config->vbill_config($JdStoreItem[0]->config_id);

        $obj = new \App\Api\Controllers\Vbill\BaseController();
        $url = $obj->alipayQueryApplyInfo;

        $data = [
            'orgId' => $vbill_config->orgId,
            'reqId' => time(),
            'version' => '1.0',
            'timestamp' => date('Ymdhis', time()),
            'signType' => 'RSA',
            'reqData' => [
                'mno' => $JdStoreItem[0]->mno,
            ],
        ];
        $res = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);

        if (empty($res['data']['respData']['infoQrcode'])) {
            return json_encode([
                'status' => 2,
                'message' => $res['data']['respData']['rejectInfo']
            ]);
        } else {
            return json_encode([
                'status' => 1,
                'data' => $res['data']['respData']['infoQrcode'],
                'message' => $res['data']['respData']['infoQrcode']
            ]);
        }
    }

    /**
     * 实名认证申请撤销
     * 2022-07-20 17:54
     */
    public function backApplyBill(Request $request)
    {
        // print_r(2);die;
        $store_id = $request->input('store_id', ''); // 门店id
        if (!$store_id) {
            return $this->errorResponse("门店Id不能为空");
        }
        $contactName = $request->input('contactName', ''); // 联系人姓名
        $phoneNumber = $request->input('phoneNumber', ''); // 联系人手机号
        $contactIdNo = $request->input('contactIdNo', ''); // 联系人身份证号

        $JdStoreItem = Db::table('vbill_stores')->where(['store_id' => $store_id])->get()->toArray();
        if (!$JdStoreItem) {
            return json_encode([
                'status' => 2,
                'message' => '随行付mno不存在'
            ]);
        }
//        print_r($JdStoreItem[0]);die;

        $config = new VbillConfigController();
        $vbill_config = $config->vbill_config($JdStoreItem[0]->config_id);
//        dump($vbill_config->sxfpublic);die;

        $obj = new \App\Api\Controllers\Vbill\BaseController();
        $url = $obj->alipayBackApplyBill;
//        dump($url);die;

//        $JdStoreItem[0]->mno = '399220517423529';
//        $JdStoreItem[0]->applicationId = 'f76c284da5644575aabe94043ccb637d';

        $data = [
            'orgId' => $vbill_config->orgId,
            'reqId' => time(),
            'version' => '1.0',
            'timestamp' => date('Ymdhis', time()),
            'signType' => 'RSA',
            'reqData' => [
//                'applicationId' => $JdStoreItem[0]->applicationId, // f76c284da5644575aabe94043ccb637d
                'mno' => $JdStoreItem[0]->mno,
            ],
        ];
        // dump($data);die;
        $res = $obj->execute($data, $url, $vbill_config->privateKey, $vbill_config->sxfpublic);
        // print_r($res);die;
        // return json_encode([
        //     'status' => 1,
        //     'message' => $res['data']['respData']['rejectInfo']
        // ]);
        // print_r($res);die;
        if ($res['data']['respData']['bizCode'] == '0000') {
            return json_encode([
                'status' => 1,
                'message' => $res['data']['respData']['bizMsg']
            ]);
        } else {
            return json_encode([
                'status' => 2,
                'message' => $res['data']['respData']['bizMsg']
            ]);
        }

    }

}